class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        h = ListNode(-1)
        h.next = head
        right, left = h, h
        k = n
        while k > 0:
            k -= 1
            right = right.next

        while right.next:
            right = right.next
            left = left.next
        ne = left.next
        left.next = ne.next
        return h.next


h = ListNode(1)
h.next = ListNode(2)
h.next.next = ListNode(3)
h.next.next.next = ListNode(4)
h.next.next.next.next = ListNode(5)
Solution().removeNthFromEnd(h, 2)

while h:
    print(h.val)
    h = h.next


